package linc.spring.demo.service;

import linc.spring.framework.BeanPostProcessor;
import linc.spring.framework.annotation.Component;

import java.lang.reflect.Proxy;

/**
 * @author yqlin
 * @date 2022/3/14 7:28 下午
 * @description
 */
@Component
public class UserBeanPostProcessor implements BeanPostProcessor {
    @SuppressWarnings("unchecked")
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        if (beanName.equals("userService")) {
            return Proxy.newProxyInstance(this.getClass().getClassLoader(), bean.getClass().getInterfaces(), (proxy1, method, args) -> {
                // 切面逻辑
                System.out.println("userService切面逻辑");
                // 执行被代理对象的方法
                return method.invoke(bean, args);
            });
        }
        return bean;
    }
}
